/**
 * 
 */
package controller;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

import model.*;
import model.parser.ParserException;
import model.turtle.Position;
import model.turtle.Turtle;
import view.Observer;
import view.SLogoView;
import view.components.ComponentsFactory;
import view.components.ErrorCatcher;

/**
 * The execute() function is provided to the view.
 */
public class Controller {
    private Model model;
    private Observer observer;
    public static final Dimension SIZE = new Dimension(600, 600);
    private SLogoView myView;
    private List<String> myCommands;
    private String copy;
    private static String PARSER_ERROR = "ParserError";
    private List<Turtle> myTurtles;

    public Controller() throws IOException {
        ComponentsFactory.loadComponents("view");
        myView = new SLogoView(ComponentsFactory.getComponentsMap().get(
                "SLogoTitle"), this);
        observer = (myView.getObserver());
        model = new Model();
        myTurtles = new ArrayList<Turtle>();

        observer.update(model.getTurtles());

        myCommands = new ArrayList<String>();
    }

    public void execute(String command) {
        myCommands.add(command);
        try {
            myTurtles = model.execute(command);
            observer.update(myTurtles);
        } catch (ParserException p) {
            new ErrorCatcher(p.getMessage());
            // new ErrorCatcher(ComponentsFactory.getComponentsMap().get(
            // PARSER_ERROR));
        }

    }

    public List<String> getHistory() {
        return myCommands;
    }

    public void setCopy(String str) {
        copy = str;
    }

    public String getCopy() {
        return copy;
    }

    public SLogoView getView() {
        return myView;
    }

}